function empPage(p){
		$.ajax({
			url : "emp/loadEmp.sls",
			type : "post",
			data : {"page":p ,"deptid":deptId},
			dataType : "json",
			success : function(result) {
				show(result);
			},
			error : function(){
				window.location.href="error.sls";
			}
		});
}

function show(result){

	$("#emp_tbody").empty();
	if(result.status == 0){
		var emps = result.data;
		for(var i=0;i<emps.length;i++){
			var empId = emps[i].empId;
			var did = emps[i].deptId;
			var roll = emps[i].job;
			var deleteElement = '<a href="javascript:emp_delet('+empId+');" class="ico del">离职</a>';
			var modifyElement = '<a href="javascript:emp_modify('+empId+');" class="ico edit">修改</a>';
			if(roll=='总裁'){
				deleteElement = '';
				modifyElement = '';
			}
			var str = empId.toString();
			
			if(str.length == 1){
				str = "00"+str;
			} else if (str.length == 2){
				str = "0"+str;
			} else {
				str = str;
			}
			var s = '<tr><td>'+str+'</td><td>'+
			emps[i].empName+'</td>'+
			'<td>'+emps[i].gender+'</td>'+
			'<td>'+emps[i].age+'</td>'+
			'<td><a style="text-decoration: none;" href="javascript:dept_detail('+did+')">'+emps[i].dName+'</a></td>'+
			'<td>'+emps[i].job+'</td>'+
			'<td>'+emps[i].hiredate+'</td>'+
			'<td>'+modifyElement+'</td>'+
			'<td>'+deleteElement+'</td>'+
			'</tr>';
			
			var $tr = $(s);
			$tr.data("empId",empId);
			$("#emp_tbody").append($tr);
		}

		$.ajax({
			url : "emp/empPage.sls",
			type : "post",
			dataType : "json",
			success : function(result) {
				if(result.status == 0 ){
					var page = result.data.page;
					var total = result.data.total;
					var top ;
					var end ;
					var pageBody = "";
					if(page == 1){
						top = '<a>上一页</a>';
					} else {
						top = '<a href="javascript:empPage('+(page-1)+');">上一页</a>';
					}
					if(page == total){
						end = '<a>下一页</a>';	
					} else {
						end = '<a href="javascript:empPage('+(page+1)+');">下一页</a>';
					}
					var i=1;
					var m=3;
					if(page>1){
						i = page-1;
						m = page+1;
					}
					if(m>total){
						i--;
						m = total;
					}
					if(i==0){
						i=1;
					}
					for(i;i<=m;i++){
						if(i==page){
							pageBody+='<a href="javascript:empPage('+i+');" style="font-weight:bold;color:#8d341f;">'+i+'</a>';
						} else {
							pageBody+='<a href="javascript:empPage('+i+');">'+i+'</a>';
						}
					}
					var s = top+pageBody+end;									
					$("#emp_page").html(s);
				}
			},
			error : function(){
				window.location.href="error.sls";
			}
		});
	}
	

}

function emp_delet(empId){
	var ok = confirm("确定要开除此员工吗?");
	if(ok){
	$.ajax({
		url : "emp/deleteEmp.sls",
		type : "post",
		data : {"id":empId},
		dataType : "json",
		success : function(result) {
			if(result.status == 0){
				empPage(result.data);
			} 
			if(result.status == 2){
				$("#emp_error").show();
				setTimeout(function(){$("#emp_error").hide();},1000);
			}
		},
		error : function(){
			window.location.href="error.sls";
		}
	});
	}
}
function dept_detail(deptId){
	window.location.href = "toEmp.sls?deptId="+deptId;
}

function searchName(){
	var name =$("#search").val();
	$.ajax({
		url : "emp/searchEmp.sls",
		type : "post",
		data : {"name":name},
		dataType : "json",
		success : function(result) {
				show(result);
		},
		error : function(){
			window.location.href="error.sls";
		}
	});
}



